﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace XFFSM
{

    [CustomEditor(typeof(FSMTransitionInspectorHelper))]
    public class FSMTransitionInspector : Editor
    {

        private ReorderableList reorderableList;

        private Rect condition_left_rect;
        private Rect condition_right_rect;
        private Rect popRect;
        //private GUIContent tempContent = new GUIContent();


        private static Dictionary<ParameterType, FSMConditionInspector> conditionInspector = new Dictionary<ParameterType, FSMConditionInspector>();

        private void OnEnable()
        {
            FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
            if (helper == null) { return; }

            reorderableList = new ReorderableList(helper.transition.conditions, typeof(FSMConditionData), true, true, true, true);
            reorderableList.onAddCallback += this.OnAddCallback;
            reorderableList.onRemoveCallback += this.OnRemoveCallback;
            reorderableList.drawElementCallback += this.DrawItem;
        }

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
            if (helper == null) { return; }

            reorderableList.list = helper.transition.conditions;
            reorderableList.DoLayoutList();

        }

        protected override void OnHeaderGUI()
        {
            //base.OnHeaderGUI();
            FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
            if (helper == null) { return; }

            GUILayout.BeginHorizontal();

            GUILayout.Label(EditorGUIUtility.IconContent("icons/processed/unityeditor/animations/animatorstatetransition icon.asset"),GUILayout.Width(30),GUILayout.Height(30));
            GUILayout.Label(string.Format("{0} -> {1}", helper.transition.fromStateName, helper.transition.toStateName));
            GUILayout.EndHorizontal();

            // 画一条 分隔的线 
            var rect = EditorGUILayout.BeginHorizontal();

            Handles.color = Color.black;
            Handles.DrawLine(new Vector2(rect.x, rect.y), new Vector3(rect.x + rect.width, rect.y));

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

        }


        private static void InitConditionInspectors() {

            if (conditionInspector.Count == 0) {
                conditionInspector.Add(ParameterType.Bool, new FSMBoolConditionInspector());
                conditionInspector.Add(ParameterType.Float, new FSMFloatConditionInspector());
                conditionInspector.Add(ParameterType.Int, new FSMIntConditionInspector());
                conditionInspector.Add(ParameterType.Trigger, new FSMConditionInspector()); // 不需要做任何绘制 
            }

        }

        private void OnAddCallback(ReorderableList list) {

            FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
            if (helper == null) { return; }
            //Debug.Log("添加条件");
            FSMConditionFactory.CreateCondition(helper.controller, helper.transition);
        }

        private void OnRemoveCallback(ReorderableList list)
        {

            FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
            if (helper == null) return;

            FSMConditionFactory.DeleteCondition(helper.controller, helper.transition,list.index);
        }

        private void DrawItem(Rect rect, int index, bool isActive, bool isFocused) {

            FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
            if (helper == null) { return; }

            var conditon = helper.transition.conditions[index];

            condition_left_rect.Set(rect.x, rect.y, rect.width / 2, rect.height);
            condition_right_rect.Set(rect.x + rect.width / 2, rect.y, rect.width / 2, rect.height);

            if (helper.controller.parameters.Count > 0)
            {
                //tempContent.text = conditon.parameterName;
                if (EditorGUI.DropdownButton(condition_left_rect, new GUIContent(conditon.parameterName), FocusType.Keyboard))
                {
                    // 弹出选择参数的弹框 TODO
                    popRect.Set(rect.x, rect.y + 2, rect.width / 2, rect.height);
                    PopupWindow.Show(popRect, new FSMSelectParamWindow(rect.width / 2, conditon, helper.controller));
                }
            }

            InitConditionInspectors();

            FSMParameterData parameter = helper.controller.parameters.Where(x => x.name.Equals(conditon.parameterName)).FirstOrDefault();

            if (parameter == null)
            {
                EditorGUI.LabelField(condition_right_rect, "缺少参数!");
            }
            else {

                // 根据不同的参数类型绘制不同的内容
                if (conditionInspector.ContainsKey(parameter.parameterType))
                    // 进行绘制
                    if(conditionInspector[parameter.parameterType] != null)
                        conditionInspector[parameter.parameterType].OnGUI(condition_right_rect, conditon, helper.controller);
                else
                    Debug.LogErrorFormat("未查询到对应的绘制方式:{0}", parameter.parameterType);
            }


        }


    }

}

